/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.settings;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.project.ex.ProjectManagerEx;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.vfs.VirtualFile;
import org.jdom.JDOMException;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

public class SettingsCheckerProjectManagerEx extends ProjectManagerEx {
    private final ProjectManagerEx delegate;

    public SettingsCheckerProjectManagerEx(ProjectManagerEx delegate) {
        this.delegate = delegate;
    }

    public Project newProject(String s, boolean flag, boolean flag1) {
        return delegate.newProject(s, flag, flag1);
    }

    public Project loadProject(String s) throws IOException, JDOMException, InvalidDataException {
        return delegate.loadProject(s);
    }

    public boolean openProject(Project project) {
        return delegate.openProject(project);
    }

    public boolean isProjectOpened(Project project) {
        return delegate.isProjectOpened(project);
    }

    public boolean canClose(Project project) {
        return delegate.canClose(project);
    }

    public void saveChangedProjectFile(VirtualFile virtualfile) {
        delegate.saveChangedProjectFile(virtualfile);
    }

    public boolean isFileSavedToBeReloaded(VirtualFile virtualfile) {
        return delegate.isFileSavedToBeReloaded(virtualfile);
    }

    public void blockReloadingProjectOnExternalChanges() {
        delegate.blockReloadingProjectOnExternalChanges();
        notifySettingsChecker(true);
    }

    public void unblockReloadingProjectOnExternalChanges() {
        delegate.unblockReloadingProjectOnExternalChanges();
        notifySettingsChecker(false);
    }

    private void notifySettingsChecker(boolean updateInProgress) {
        Project[] projects = getOpenProjects();
        for (int i = 0; i < projects.length; i++) {
            Project project = projects[i];
            SettingsChecker checker = SettingsChecker.getInstance(project);
            checker.updateInProgress(updateInProgress);
        }
    }

    public void addProjectManagerListener(ProjectManagerListener listener) {
        delegate.addProjectManagerListener(listener);
    }

    public void removeProjectManagerListener(ProjectManagerListener listener) {
        delegate.removeProjectManagerListener(listener);
    }

    public void addProjectManagerListener(Project project, ProjectManagerListener listener) {
        delegate.addProjectManagerListener(project, listener);
    }

    public void removeProjectManagerListener(Project project, ProjectManagerListener listener) {
        delegate.removeProjectManagerListener(project, listener);
    }

    public Project[] getOpenProjects() {
        return delegate.getOpenProjects();
    }

    public Project getDefaultProject() {
        return delegate.getDefaultProject();
    }

    @Nullable
    public Project loadAndOpenProject(String filePath) throws IOException, JDOMException, InvalidDataException {
        return delegate.loadAndOpenProject(filePath);
    }

    public boolean closeProject(Project project) {
        return delegate.closeProject(project);
    }

    public void reloadProject(Project project) {
        delegate.reloadProject(project);
    }
}
